package nl.depository.pim.pages.admin;

import java.util.List;

import nl.depository.pim.services.DataService;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

public class Overview
{
	@Property(write=false)
	private String classSimpleName;
	
	private Class<?> beanClass;
	
	@SuppressWarnings("unused")
	@Property(write=false)
	private List<Object> beans;
	
	@Property
	private Object bean;

	@Inject
	private DataService dataService;
	
	public void onActivate(String classSimpleName) throws Exception
	{
		this.classSimpleName = classSimpleName;
		
		beanClass = Class.forName("nl.depository.pim.entities." + classSimpleName);
	}
	
	public String onPassivate()
	{
		return classSimpleName;
	}

	public String getId() throws Exception
	{
		return (String) beanClass.getDeclaredMethod("getId").invoke(bean);
	}
	
	public void setupRender()
	{
		beans = dataService.query("SELECT FROM " + classSimpleName);
	}
	
	public void onDelete(String id) throws Exception
	{
		dataService.delete(id);
	}
}
